package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/cart")
public class CartApiController {

    @Autowired
    private CartService cartService;

    @RequestMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum,
                            HttpServletRequest request){

        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            // 未登录添加购物车 获取一个临时用户Id 存储到cookie中
           userId = AuthContextHolder.getUserTempId(request);

        }
        //调用服务层方法
        this.cartService.addToCart(skuId,userId,skuNum);
        return Result.ok();
    }

    //查看购物车列表
    @GetMapping("cartList")
    public Result getCartList(HttpServletRequest request){
        //获取用户id
        String userId = AuthContextHolder.getUserId(request);
        //获取临时用户Id
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartInfoList = this.cartService.getCartList(userId,userTempId);
        return Result.ok(cartInfoList);
    }

    //选中状态
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request){
        //获取用户id
        String userId = AuthContextHolder.getUserId(request);

        if (StringUtils.isEmpty(userId)){
            //获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        this.cartService.checkCart(skuId,userId,isChecked);
        return Result.ok();
    }

    //删除购物项
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId,
                             HttpServletRequest request){
        //获取用户id
        String userId = AuthContextHolder.getUserId(request);

        if (StringUtils.isEmpty(userId)){
            //获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        this.cartService.deleteCart(skuId,userId);
        return Result.ok();
    }

    //根据用户Id 查询购物车列表
    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId){
        return cartService.getCartCheckedList(userId);
    }

    //反选购物车
    @GetMapping("allCheckCart/{isChecked}")
    public Result allCheckCart(@PathVariable Integer isChecked,HttpServletRequest request){
        //获取用户id
        String userId = AuthContextHolder.getUserId(request);
        //获取临时用户Id
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        this.cartService.allCheckCart(userId,isChecked);
        return Result.ok();
    }
    //清空购物车
    @GetMapping("clearCart")
    public Result clearCart(HttpServletRequest request){
        //获取用户id
        String userId = AuthContextHolder.getUserId(request);
        //获取临时用户Id
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        this.cartService.clearCart(userId);
        return Result.ok();
    }

}
